
using System;
using System.IO;

using iTextSharp.text;
using iTextSharp.text.pdf;

public class Sharp0510
{
    public static void _Main()
    {
        Console.WriteLine("Chapter 5 example 10: large tables with repeating headers");
        // creation of the document with a certain size and certain margins
        Document document = new Document(PageSize.A4.Rotate(), 50, 50, 50, 50);
        try
        {
            // creation of the different writers
            PdfWriter.GetInstance(document, new FileStream("Sharp0510.pdf", FileMode.Create));
            // we add some meta information to the document
            document.AddAuthor("Gerald Henson");
            document.AddSubject("This is the result of a Test.");
            document.Open();
            PdfPTable datatable = new PdfPTable(10);
            datatable.DefaultCell.Padding = 4;
            //datatable.DefaultCell.Spacing = 0;
            //datatable.setBorder(Rectangle.NO_BORDER);
            float[] headerwidths = { 10, 24, 12, 12, 7, 7, 7, 7, 7, 7 };
            datatable.SetWidths(headerwidths);
            datatable.WidthPercentage = 100;
            // the first cell spans 10 columns
            PdfPCell cell = new PdfPCell(new Phrase("Administration -System Users Report", FontFactory.GetFont(FontFactory.HELVETICA, 24, Font.BOLD)));
            cell.HorizontalAlignment = Element.ALIGN_CENTER;
            cell.SetLeading(30, 0);
            cell.Colspan = 10;
            cell.Border = Rectangle.NO_BORDER;
            cell.BackgroundColor = new Color(0xC0, 0xC0, 0xC0);
            datatable.AddCell(cell);
            // These cells span 2 rows
            datatable.DefaultCell.BorderWidth = 2;
            datatable.HorizontalAlignment = 1;
            datatable.DefaultCell.Rowspan = 2;
            datatable.AddCell("User Id");
            datatable.AddCell(new Phrase("Name", FontFactory.GetFont(FontFactory.HELVETICA, 14, Font.BOLD)));
            datatable.AddCell("Company");
            datatable.AddCell("Department");
            // This cell spans the remaining 6 columns in 1 row
            datatable.DefaultCell.Rowspan = 1;
            datatable.DefaultCell.Colspan = 6;
            datatable.AddCell("Permissions");
            // These cells span 1 row and 1 column
            datatable.DefaultCell.Colspan = 1;
            datatable.AddCell("Admin");
            datatable.AddCell("Data");
            datatable.AddCell("Expl");
            datatable.AddCell("Prod");
            datatable.AddCell("Proj");
            datatable.AddCell("Online");
            // this is the end of the table header

            datatable.DefaultCell.BorderWidth = 1;
            datatable.DefaultCell.Rowspan = 1;
            for (int i = 1; i < 30; i++)
            {
                datatable.HorizontalAlignment = Element.ALIGN_LEFT;
                datatable.AddCell("myUserId");
                datatable.AddCell("Somebody with a very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very long long name");
                datatable.AddCell("No Name Company");
                datatable.AddCell("D" + i);
                datatable.HorizontalAlignment = Element.ALIGN_CENTER;
                datatable.AddCell("No");
                datatable.AddCell("Yes");
                datatable.AddCell("No");
                datatable.AddCell("Yes");
                datatable.AddCell("No");
                datatable.AddCell("Yes");
            }

            document.Add(datatable);
        }
        catch (Exception e)
        {
            Console.Error.WriteLine(e.StackTrace);
        }
        // we close the document
        document.Close();
    }
}